package com.sgcc.epri.dcloud.controller;

import com.sgcc.epri.dcloud.pojo.DataMaintenance;
import com.sgcc.epri.dcloud.pojo.Result;
import com.sgcc.epri.dcloud.service.DataMaintenanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/dataMaintenance")
@Api(description = "数据维护")
public class DataMaintenanceController {

    @Autowired
    private DataMaintenanceService dataMaintenanceService;

    @GetMapping("/select")
    @ApiOperation("返回的树状结构")
    public Result selectData(HttpServletRequest request, HttpServletResponse response) {
//        String remoteAddr = "";
//
//        if (request != null) {
//            remoteAddr = request.getHeader("X-FORWARDED-FOR");
//            if (remoteAddr == null || "".equals(remoteAddr)) {
//                remoteAddr = request.getRemoteAddr();
//            }
//        }
//        System.out.println(remoteAddr);
        return dataMaintenanceService.selectData();
    }

    @PostMapping("/insert")
    @ApiOperation("新增一级时，parent_id为0，level为1,添加子集时level应为2")
    private Result insertData(@RequestBody DataMaintenance dataMaintenance){
        return dataMaintenanceService.insertData(dataMaintenance);
    }

    @PostMapping("/update")
    public Result updateData(@RequestBody DataMaintenance dataMaintenance){
        return dataMaintenanceService.updateData(dataMaintenance);
    }

    @DeleteMapping("/delete")
    private Result deleteData(@RequestParam(name = "id", required = true) @ApiParam(required = true) String id){
        return dataMaintenanceService.deleteData(id);
    }

    @GetMapping("/upOrDown")
    @ApiOperation("上移或下移")
    public Result upOrDown(@RequestParam(value = "hostId",required = true) @ApiParam(value = "主id 初始位置", name = "hostId", required = true) String hostId,
                           @RequestParam(value = "rmId",required = true) @ApiParam(value = "次id 目标位置", name = "rmId", required = true) String rmId){
        return dataMaintenanceService.upOrDown(hostId, rmId);
    }

}
